package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 新唐人
 * @author KK
 *
 */
public class PubtopicNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.pubtopic.org/News/[a-zA-Z0-9]{10,}.html",
	};


	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("#ContentPlaceHolder1_Titles").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("#content-text  p"));
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#contentText"));
		}
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#ContentPlaceHolder1_Content"));
		}
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".Article_Content p"));
		}

		date=JSoupUtils.matchDate(doc, "发布日期:");
		author=JSoupUtils.matchAuthor(doc, "来源:");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		PubtopicNewsAnalyse analyse=new PubtopicNewsAnalyse();
		String url="http://www.pubtopic.org/News/9d98fdba125976aa354ae1efeda549c7.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
